package org.jz.gae.redbook.spb.client.slate.family;

import org.jz.gae.redbook.spb.client.toolkit.Slate;
import org.jz.gae.redbook.spb.shared.FamilyEntityView;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FamilyDetailPlate extends Composite
{
      private VerticalPanel rootPanel = new VerticalPanel();
	  private final Button editButton = new Button("Изменить");
	  private final Button deleteButton = new Button("Удалить");;
	  private final Button closeButton = new Button("Закрыть");;
	  private FamilyPropertiesPanel familyPropertiesPanel = new FamilyPropertiesPanel();
	  private Slate slate;
	  private FamilyEntityView entityView;
      private ClickHandler changeEvent; 


	public FamilyDetailPlate() 
	{
		SimplePanel contentTableDecorator = new SimplePanel();
	  initWidget(contentTableDecorator);
	  contentTableDecorator.setWidth("100%");
	  initilizeInterface();
      contentTableDecorator.add(rootPanel);
      
      editButton.addClickHandler(new ClickHandler(){
    	  @Override
    	  public void onClick(ClickEvent event)
          {
              editFamily();
    	  }
      });
      deleteButton.addClickHandler(new ClickHandler(){
    	  @Override
    	  public void onClick(ClickEvent event)
          {
              deleteFamily();
    	  }
      });
      closeButton.addClickHandler(new ClickHandler(){
    	  @Override
    	  public void onClick(ClickEvent event)
          {
              closePlate();
    	  }
      });
	}

	private void initilizeInterface()
	{
		rootPanel.setWidth("100%");
		familyPropertiesPanel.setWidth("100%");
		rootPanel.add(familyPropertiesPanel);
	    HorizontalPanel menuPanel = new HorizontalPanel();
	    menuPanel.add(editButton);
	    menuPanel.add(deleteButton);
	    menuPanel.add(closeButton);
	    rootPanel.add(menuPanel);
	    familyPropertiesPanel.lockInterface();
	}

	public void openEntityView(
			Slate _Slate,
		    String _FamilyId,	
			ClickHandler _ChangeEvent)
	{
		slate = _Slate;
		slate.getContainerAgent().getRpcService().getFamily(_FamilyId, new AsyncCallback<FamilyEntityView>(){
			@Override
			public void onFailure(Throwable caught)
			{
				Window.alert("Ошибка загрузки семейства : " + caught.getMessage());
			}

			@Override
			public void onSuccess(FamilyEntityView result)
			{
				entityView = result;
				familyPropertiesPanel.displayInformation(entityView);
			}
		});
		changeEvent = _ChangeEvent;
		slate.exposePlate(this);
	}
	
	private void closePlate()
	{
		slate.closeTopPlate();
	}
	
	private void editFamily()
	{
		try
		{
            String id = entityView.getFamilyKey();
            /**/
            FamilyEditPlate editPlate = new FamilyEditPlate();
            editPlate.editEntityView(slate, id, new ClickHandler()
			{
				@Override
				public void onClick(ClickEvent event)
				{
					slate.closeTopPlate();
				}
			});
		}
		catch(Exception ex)
		{
            Window.alert("Invalid data : " + ex.getMessage());
		}
		
	}
	
	private void deleteFamily()
	{
		Window.alert("Not yet implemented");
		slate.closeTopPlate();
	}
}